package com.xyht.sca_s.student_manage_system.modules.workApplication.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.workApplication.entity.vo.*;
import com.xyht.sca_s.student_manage_system.modules.workApplication.service.SmsWorkApplicationService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.Future;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2023-05-17
 */
@RestController
@RequestMapping("/workApplication/smsWorkApplication")
@SecretBody
public class SmsWorkApplicationController {
    @Autowired
    SmsWorkApplicationService smsWorkApplicationService;
    @Autowired
    JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "新增工作请示单")
    @PostMapping("/add")
    public ResponseResult smsWorkApplicationAdd(@RequestBody SmsWorkApplicationVO smsWorkApplicationVO,
                                                HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.smsWorkApplicationAdd(smsWorkApplicationVO,user_id);
    }

    @ApiModelProperty(value = "修改工作请示单")
    @PostMapping("/update")
    public ResponseResult smsWorkApplicationUpdate(@RequestBody SmsWorkApplicationVO smsWorkApplicationVO,
                                                HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.smsWorkApplicationUpdate(smsWorkApplicationVO,user_id);
    }

    @ApiModelProperty(value = "撤销工作请示单")
    @PostMapping("/cancel")
    public ResponseResult smsWorkApplicationCancel(@RequestBody SmsWorkApplicationCancelVO smsWorkApplicationCancelVO,
                                                HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.smsWorkApplicationCancel(smsWorkApplicationCancelVO,user_id);
    }

    @ApiModelProperty(value = "审批")
    @PostMapping("/allow")
    public ResponseResult allowSmsWorkApplication(@RequestBody SmsWorkApplicationAllowVO allowVO, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.allowSmsWorkApplication(allowVO, user_id);
    }

    @ApiModelProperty(value = "审批列表")
    @GetMapping("/list")
    public ResponseResult getSmsWorkApplicationList(
                                                @RequestParam(value = "search_str",required = false) String search_str,
                                                @RequestParam(value = "allowStatus",required = false) Integer allowStatus,
                                                @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "application_type") Integer application_type,
                                                HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.getSmsWorkApplicationList(search_str,allowStatus,user_id,application_type,pageNum,pageSize);
    }

    @ApiModelProperty(value = "审批详情")
    @GetMapping("/detail")
    public ResponseResult getSmsWorkApplicationDetail(@RequestParam(value = "application_id") String application_id,
                                                  HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.getSmsWorkApplicationDetail(application_id,user_id);
    }

    @ApiModelProperty(value = "申请人指定流程（申请前获取）")
    @GetMapping("/processList")
    public ResponseResult getSmsWorkApplicationOriginSettingProcess() {
        return smsWorkApplicationService.getSmsWorkApplicationOriginSettingProcess();
    }
    @ApiModelProperty(value = "上传附件")
    @PostMapping("/upLoad")
    public ResponseResult upLoadFile(@RequestParam("file") MultipartFile file){
        return smsWorkApplicationService.upLoadFile(file);
    }
    @ApiModelProperty(value = "下载文件")
    @GetMapping("/download/{id}")
    public ResponseResult downloadFileManagement(@PathVariable("id") String id, HttpServletResponse response){
        return smsWorkApplicationService.downloadFileManagement(id,response);
    }
    @ApiModelProperty(value = "查询经办人")
    @GetMapping("/select")
    public ResponseResult selectAgent(@RequestParam(value = "person_name", required = false) String person_name){
        return smsWorkApplicationService.selectAgent(person_name);
    }

    @ApiModelProperty(value = "文件下载")
    @GetMapping("/download")
    public ResponseResult tableDownload(@RequestParam(value = "apply_id") String apply_id,
                                        HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsWorkApplicationService.tableDownload(apply_id,response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }
    @ApiModelProperty(value = "查询记录")
    @GetMapping("/selectAll")
    public ResponseResult selectAllApply(
            @RequestParam(value = "apply_unit",required = false) String apply_unit,
            @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            @RequestParam(value = "status",required = false) Integer status
    ){
        return smsWorkApplicationService.selectAllApply(apply_unit,
                pageSize,pageNum,status);
    }

    @ApiOperation("批量导出")
    @PostMapping("/batch/export")
    public ResponseResult exportWorkApplication(@RequestBody SmsWorkApplicationExportVO exportVO,
                                                     HttpServletResponse response){
        ResponseResult responseResult;
        try{
            Future<ResponseResult> future = smsWorkApplicationService.exportWorkApplication(exportVO, response);
            while (true){
                if (future.isDone()){
                    break;
                }
            }
            responseResult = future.get();
        }catch (Exception e){
            e.printStackTrace();
            responseResult = CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return responseResult;
    }

    @ApiModelProperty(value = "强制归档")
    @PostMapping("/forced")
    public ResponseResult forcedFinishWorkGroupApplication(@RequestBody SmsWorkApplicationForceVO forceVO,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.forcedFinishWorkGroupApplication(forceVO.getApplyId(), userId);
    }

    @ApiModelProperty(value = "强制流转")
    @PostMapping("/forcedTorsion")
    public ResponseResult forcedTorsionWorkGroupApplication(@RequestBody SmsWorkApplicationDetailTorsionVO torsionVO){
        return smsWorkApplicationService.forcedTorsionWorkGroupApplication(torsionVO);
    }

    @ApiModelProperty(value = "代审批")
    @PostMapping("/replaceApply")
    public ResponseResult replaceApplyWorkGroupApplication(HttpServletRequest request,
                                                     @RequestBody SmsWorkApplicationAllowVO allowVO){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.replaceApplyWorkGroupApplication(userId,allowVO);
    }

    @ApiModelProperty(value = "强制驳回")
    @PostMapping("/forcedReject")
    public ResponseResult forcedRejectWorkGroupApplication(@RequestBody SmsWorkApplicationForceVO forceVO,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.forcedRejectWorkGroupApplication(forceVO, userId);
    }

    @ApiModelProperty(value = "删除工作请示单")
    @PostMapping("/deleted")
    public ResponseResult deletedWorkApplication(@RequestParam("applyId") String applyId){
        return smsWorkApplicationService.deletedWorkApplication(applyId);
    }

    @ApiModelProperty(value = "修改审批意见工作")
    @PostMapping("/updateOpinion")
    public ResponseResult updateOpinion(@RequestBody SmsWorkApplicationUpdateOpinionVo updateOpinionVo,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.updateOpinion(updateOpinionVo,userId);
    }

    @ApiModelProperty(value = "修改附件")
    @PostMapping("/updateAnnex")
    public ResponseResult updateAnnex(@RequestBody SmsWorkApplicationUpdateAnnexVo updateAnnexVo,HttpServletRequest request){
        String userId = jwtTokenUtil.getUserIdByRequest(request);
        return smsWorkApplicationService.updateAnnex(updateAnnexVo,userId);
    }
}

